using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Http.Dispatcher;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using log4net;
using SmartLink.Application.Services;
using SmartLink.Infrastructure.Mappers;
using SmartLink.WebApi.App_Start;
using Quartz;
using Quartz.Impl;
using Quartz.Simpl;
using Quartz.Xml;
using SmartLink.Infrastructure;
using SmartLink.Infrastructure.Logger;

namespace SmartLink.WebApi
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        IScheduler sched;

        protected void Application_Start()
        {
            var collection=log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));
            Log4NetLogger logger = new Log4NetLogger();
            logger.Info("项目启动");
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AutoMapperConfiguration.Configure();
            //ModelBinders.Binders.DefaultBinder = (IModelBinder)new UtcDateTimeModelBinder();

            //GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerSelector),
            //                                       new AreaHttpControllerSelector(
            //                                           GlobalConfiguration.Configuration));

            App_Start.Bootstrapper.run();
            //初始化缓存数据
            AppStartService.InitCache();

            //Quartz定时任务
            //XMLSchedulingDataProcessor processor = new XMLSchedulingDataProcessor(new SimpleTypeLoadHelper());
            ////创建一个作业调度池
            //ISchedulerFactory schedf = new StdSchedulerFactory();
            //sched = schedf.GetScheduler();

            ////从配置文件中设置任务和触发器 并组装到调度中
            //processor.ProcessFileAndScheduleJobs(IOHelper.GetMapPath("/quartz_jobs.xml"), sched);

            ////开始运行
            //sched.Start();

            ////创建出来一个具体的作业
            ////---开始创建任务---;
            //IJobDetail job = JobBuilder.Create<JobDemo>().Build();
            ////创建并配置一个触发器
            ////---开始创建触发器（每3秒运行一次）---;
            //ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create().WithSimpleSchedule(x => x.WithIntervalInSeconds(3).WithRepeatCount(int.MaxValue)).Build();
            ////加入作业调度池中
            //sched.ScheduleJob(job, trigger);


        }

        private void Application_End()
        {
            //停止运行
            if (sched != null)
            {
                sched.Shutdown(true);
            }
        }
    }
}
